/////////////////////////////////////////////////////////////////////////
//Module Name:
//
//    CANDll.h  
//
//Abstract:   CAN interface.
//
//Environment:
//
//    Windows2000/Windows XP
//
//Author:    mz.yang  august, 2007
//
/////////////////////////////////////////////////////////////////////////

#include <windows.h>

#ifndef _CANDll_H_
#define _CANDll_H_

/////////////////////////////////////////////////////////////////////////
//
// constant define .
//
/////////////////////////////////////////////////////////////////////////

#define NO_ACTION						255

// CAN work mode,"BasicCAN" or "PeliCAN"
#define BASICCAN_MODE					1
#define PELICAN_MODE					2

// sleep or wake up
#define WAKE_UP_MODE					1
#define SLEEP_MODE						2

// "PeliCAN" mode
#define DUAL_ACC_FILTER_MODE			0
#define SINGLE_ACC_FILTER_MODE			1
#define SELF_TEST_MODE					1
#define LISTEN_ONLY_MODE				1

// baud rate
#define	BAUD_RATE_5K					5
#define	BAUD_RATE_10K					10
#define	BAUD_RATE_20K					20
#define	BAUD_RATE_40K					40
#define	BAUD_RATE_50K					50
#define	BAUD_RATE_80K					80
#define	BAUD_RATE_100K					100
#define	BAUD_RATE_125K					125
#define	BAUD_RATE_200K					200
#define	BAUD_RATE_250K					250
#define	BAUD_RATE_400K					400
#define	BAUD_RATE_500K					500
#define	BAUD_RATE_666K					666
#define	BAUD_RATE_800K					800
#define	BAUD_RATE_1000K					1000

// default OCR
#define DEFAULT_OUT_CONTROL				0x1a

// transmit type
#define TRANSMISSION_REQUEST			1
#define SINGLE_SHOT_REQUEST				2
#define SELF_RECEIPTION_REQUEST			3
#define SINGLE_SHOT_AND_SELF_RECEIPTION_REQUEST	4

// frame format
#define STAANDARD_FRAME					1
#define EXTEND_FRAME					2

// frame type
#define DATA_FRAME						1
#define REMOTE_FRAME					2

// buffer type
#define TRANSMIT_BUFFER					1
#define RECEIVE_BUFFER					2

// error type
#define ERROR_WARNING					1
#define ERROR_PASSIVE					2
#define	BUS_OFF							3
#define ARBITRATION_LOST				4
#define DATA_OVERRUN					5


/////////////////////////////////////////////////////////////////////////
//
// structure.
//
////////////////////////////////////////////////////////////////////////

// CAN init information structure.
// Members:
//		Mode1		-	It corresponding the constant as
//							BASICCAN_MODE : "BasicCAN".
//							PELICAN_MODE  : "PeliCAN".
//		Mode2		-	It corresponding the constant as
//		BaudRate
//		Filter
//		AccCode
//		AccMask
//		OutControl
//
typedef struct _CAN_INIT_INFO {
    UCHAR	Mode;				// "BasicCAN" or "PeliCAN".
	UCHAR	SleepFlag;			// sleep,self test,listen only,reset
	UCHAR	FilterFlag;			// filter mode,only for "PeliCAN",in "BasicCAN" is invalid.
	UCHAR	SelfTestFlag;		//
	UCHAR	ListenOnlyFlag;		//
	USHORT	BaudRate;			// baud rate.when this parameters set to "0",BTR0,BTR1 will be used.
	UCHAR	BTR0;				// bus timing 0.this parameters is valid when "BaudRate" set to "0",otherwise,is invalid.
	UCHAR	BTR1;				// bus timing 1.this parameters is valid when "BaudRate" set to "0",otherwise,is invalid.
	ULONG	AccCode;			// acceptance code.
	ULONG	AccMask;			// acceptance mask.
	UCHAR	OutControl;			// output control.
	ULONG	TransBufferSize;	// transmit buffer size,as frame numbers.
	ULONG	RecBufferSize;	// receive buffer size,as frame numbers.
} CAN_INIT_INFO, *PCAN_INIT_INFO;

// Transmit or receive data information structure.
// Members:
//		TransmitType
//		FrameFormat
//		FrameType
//		ID
//		lpBuffer
//		DataLength
//		
typedef struct _CAN_DATA_INFO {
	UCHAR	TransmitType;		// transmit type.
	UCHAR	FrameFormat;		// frame format.
	UCHAR	FrameType;			// frame type.
	ULONG	ID;					// frame ID.
	PUCHAR	LPBuffer;			// transmit: point to the data need transmit.
								// receive: point to save data buffer.  
	DWORD	DataLength;			// transmit: transmit data length.
								// receive: save data buffer length,when data received,
								//			modified as infact data length received.  
} CAN_DATA_INFO, *PCAN_DATA_INFO;

// CAN Error information structure.
// Members:
//	
//
// 
typedef struct _CAN_ERROR_INFO {
    UCHAR	ErrType;			// error type.
	UCHAR	ErrCapCode;			// error capture code.
	UCHAR	ArLostErrData;		// arbitration lost capture code.
	UCHAR	RxErrCounter;		// RX error counter.
	UCHAR	TxErrCounter;		// TX error counter.
} CAN_ERROR_INFO,*PCAN_ERROR_INFO;

// CAN status information structure.
// Members:
//
//
//
typedef struct _CAN_STATUS_INFO {
    UCHAR	RegMode;			// mode register.
    UCHAR	RegStatus;			// staus register.
	UCHAR	RegALCapture;		// arbitration lost capture register.
	UCHAR	RegECCapture;		// error code capture register.
	UCHAR	RegEWLimit;			// error warning limit register.
	UCHAR	RegRECounter;		// RX error counter.
	UCHAR	RegTECounter;		// TX error counter.
} CAN_STATUS_INFO,*PCAN_STATUS_INFO;
#if 0

/////////////////////////////////////////////////////////////////////////
//Function Description:
//	Open CAN channel.
//
//Parameters:
//
//	ChannelsIndex	- CAN channels number,start with 1,as 1,2,3...
//	hReceiveEvent	- points to receive event handle.
//
//Return Value:
//	device handle			- Open CAN channel successed.
//	INVALID_HANDLE_VALUE	- Open CAN channel failed.
//			
/////////////////////////////////////////////////////////////////////////
HANDLE _stdcall CANOpen(int ChannelsIndex,HANDLE *hReceiveEvent);

/////////////////////////////////////////////////////////////////////////
//Function Description:
//	Close CAN channel.
//
//Parameters:
//	hDevice			- device handle returned by CANOpen.
//	hReceiveEvent	- receive event handle returned by CANOpen.
//
//Return Value:
//	0		- Close CAN channel successed.
//	-1		- Close CAN channel failed.
//			
/////////////////////////////////////////////////////////////////////////
int _stdcall CANClose(HANDLE hDevice,HANDLE hReceiveEvent);

/////////////////////////////////////////////////////////////////////////
//Function Description:
//	Init CAN controller.
//
//Parameters:
//	hDevice			- device handle returned by CANOpen
//	pCANInitInfo	- Point to CAN a CAN_INIT_INFO structure.
//
//Return Value:
//	0		- Init CAN controller successed.
//	-1		- Init CAN controller failed.
//			
/////////////////////////////////////////////////////////////////////////
int _stdcall CANInit(HANDLE hDevice,PCAN_INIT_INFO pCANInitInfo);


/////////////////////////////////////////////////////////////////////////
//Function Description:
//	Get CAN controller reference.
//
//Parameters:
//	hDevice			- device handle returned by CANOpen.
//	CANAddress		- CAN controller register address.
//	pValue			- buffer which save register value.
//
//Return Value:
//	0		- get reference successed.
//	-1		- get reference failed.
//			
/////////////////////////////////////////////////////////////////////////
int _stdcall CANGetRegister(HANDLE hDevice,unsigned short CANAddress,byte *pValue);


/////////////////////////////////////////////////////////////////////////
//Function Description:
//	Set CAN controller reference.
//
//Parameters:
//	hDevice			- device handle returned by CANOpen.
//	CANAddress		- CAN controller register address.
//	Value			- register value need set.
//
//Return Value:
//	0		- set reference successed.
//	-1		- set reterence failed.
//			
/////////////////////////////////////////////////////////////////////////
int _stdcall CANSetRegister(HANDLE hDevice,unsigned short CANAddress,byte Value);


/////////////////////////////////////////////////////////////////////////
//Function Description:
//	Start CAN.
//
//Parameters:
//	hDevice		- device handle returned by CANOpen.
//
//Return Value:
//	0		- Start CAN successed.
//	-1		- Start CAN failed.
//			
/////////////////////////////////////////////////////////////////////////
int _stdcall CANStart(HANDLE hDevice);


/////////////////////////////////////////////////////////////////////////
//Function Description:
//	Reset CAN.
//
//Parameters:
//	hDevice			- device handle returned by CANOpen.
//
//Return Value:
//	0		- Reset CAN successed.
//	-1		- Reset CAN failed.
//			
/////////////////////////////////////////////////////////////////////////
int _stdcall CANReset(HANDLE hDevice);


/////////////////////////////////////////////////////////////////////////
//Function Description:
//	Transmit data.
//
//Parameters:
//	hDevice			- device handle returned by CANOpen.
//	pCANDataInfo	- Point to CAN a CAN_DATA_INFO structure.
//	Length			- PCAN_DATA_INFO array length.
//
//Return Value:
//  0xffffffff		- transmit data failed.
//	others			- Infact frame numbers to transmited.
//			
/////////////////////////////////////////////////////////////////////////
DWORD _stdcall CANTransmit(HANDLE hDevice,PCAN_DATA_INFO pTransCANDataInfo,DWORD Length);


/////////////////////////////////////////////////////////////////////////
//Function Description:
//	Receive data.
//
//Parameters:
//	hDevice			- device handle returned by CANOpen.
//	pCANDataInfo	- Point to CAN a CAN_DATA_INFO structure.
//	Length			- PCAN_DATA_INFO array length.
//	WaitTime		- if no data in driver buffer at this moment,function wait for 
//					  "WaitTime" millisecond.then no data in driver buffer,
//					  this funtion will be return.
//
//Return Value:
//	0xffffffff		- receiceive data failed.
//	others			- Infact numbers of bytes to received.
//			
/////////////////////////////////////////////////////////////////////////
DWORD _stdcall CANReceive(HANDLE hDevice,PCAN_DATA_INFO pRcvCANDataInfo,DWORD Length,unsigned long WaitTime);


/////////////////////////////////////////////////////////////////////////
//Function Description:
//	Stop CAN transmit.
//
//Parameters:
//	hDevice			- device handle returned by CANOpen.
//
//Return Value:
//	0		- Stop CAN transmit successed.
//	-1		- Stop CAN transmit failed.
//			
/////////////////////////////////////////////////////////////////////////
int _stdcall CANStop(HANDLE hDevice);


/////////////////////////////////////////////////////////////////////////
//Function Description:
//	Read last error infomation.
//
//Parameters:
//	hDevice			- device handle returned by CANOpen.
//	pCANErrorInfo	- Point to CAN a CAN_ERROR_INFO structure.
//
//Return Value:
//	0		- Read last error information successed.
//	-1		- Read last error information failed.
//			
/////////////////////////////////////////////////////////////////////////
int _stdcall CANGetLastErrorInfo(HANDLE hDevice,PCAN_ERROR_INFO pCANErrorInfo);


/////////////////////////////////////////////////////////////////////////
//Function Description:
//	Read CAN controller current status infomation.
//
//Parameters:
//	hDevice			- device handle returned by CANOpen.
//	pCANStatusInfo	- Point to CAN a CAN_STATUS_INFO structure.
//
//Return Value:
//	0		- Read CAN controller current status infomation successed.
//	-1		- Read CAN controller current status infomation failed.
//			
/////////////////////////////////////////////////////////////////////////
int _stdcall CANGetStatusInfo(HANDLE hDevice,PCAN_STATUS_INFO pCANStatusInfo);


/////////////////////////////////////////////////////////////////////////
//Function Description:
//	Clear CAN transmit or receive buffer.
//
//Parameters:
//	hDevice			- device handle returned by CANOpen.
//	BufferType	- "1" is transmit buffer,"2" is receive buffer.
//
//Return Value:
//	0		- Reset CAN successed.
//	-1		- Reset CAN failed.
//			
/////////////////////////////////////////////////////////////////////////
int _stdcall CANClearBuffer(HANDLE hDevice,int BufferType);
#endif

#endif